package org.tinygroup.springmvc.util;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.http.MediaType;
import org.tinygroup.commons.tools.StringUtil;

public final class MediaTypeUtil {

	private static final String PARAM_QUALITY_FACTOR = "q";
	
	/**
	 * Return a replica of this instance with the quality value of the given MediaType.
	 * @return the same instance if the given MediaType doesn't have a quality value, or a new one otherwise
	 */
	public static MediaType copyQualityValue(MediaType acceptType,MediaType produceType) {
		String parameter = acceptType.getParameter(PARAM_QUALITY_FACTOR);
		if (StringUtil.isBlank(parameter)) {
			return produceType;
		}
		Map<String, String> params = new LinkedHashMap<String, String>();
		params.put(PARAM_QUALITY_FACTOR, parameter);
		return new MediaType(produceType, params);
	}
	
}
